{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Self Driving Car 2D World\n",
    "\n",
    "In the code cell, you'll find skeleton code for a class called SelfDrivingCar. \n",
    "\n",
    "\n",
    "The SelfDrivingCar class is initialized with the number of rows and number of columns in a 2D grid.\n",
    "\n",
    "The class also contains four methods. You will need to implement these three methods\n",
    "* initialize_grid() - calculates the initial probabilities of each square on the grid\n",
    "* output_probability() - outputs the probability at a specific point on the grid\n",
    "* update_probability() - updates the probabilities at specific points on the grid\n",
    "\n",
    "The fourth method, visualize_probability(),visualizes the vehicle's 2D grid. This method is already provided for you.\n",
    "\n",
    "Follow the TODOs in the code. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from pandas import DataFrame\n",
    "\n",
    "class SelfDrivingCar():\n",
    "    def __init__(self, rows, columns):\n",
    "        \n",
    "        # initializes a map as a list\n",
    "        self.grid = []\n",
    "\n",
    "        ### TODO:\n",
    "        # initialize variables\n",
    "        # self.grid_size is a list containing the number of rows\n",
    "        # and number of columns in the grid like [10,3]. Use the rows and \n",
    "        # columns input variables to define self.grid_size\n",
    "        self.grid_size = 0\n",
    "        \n",
    "        ### TODO: \n",
    "        # store the total number of elements in the grid. The number\n",
    "        # of elements would be the rows * columns\n",
    "        self.num_elements = 0\n",
    "        \n",
    "    ### TODO:\n",
    "    # write the function that initializes the grid. Remember that\n",
    "    # when the robot turns on, it has no idea where it is. So if there\n",
    "    # are 25 points on the grid, the initial probability of each point\n",
    "    # is 1/25.\n",
    "    # You will create a 2-D map using a python list. This can be\n",
    "    # a bit tricky, and you might have to search online for how to\n",
    "    # program a 2-D list in python. A 2-D list will need a for loop\n",
    "    # within a for loop\n",
    "    \n",
    "    def initialize_grid(self):\n",
    "        \n",
    "        ### TODO: \n",
    "        # calculate the probability of being at any element on the grid\n",
    "        # you can use the self.num_elements variable you defined in the\n",
    "        # __init__ function\n",
    "        probability = 0\n",
    "        \n",
    "        ### TODO:\n",
    "        # write a for loop to fill out the 2-D map with the value in the\n",
    "        # probability variable. For example, if the map has 25 points,\n",
    "        # the map should be initialized to map[0,0] = 0.04 \n",
    "        # map[0,1] = 0.04\n",
    "        # map[0, 2] = 0.04\n",
    "        # etc.\n",
    "        # python''s list.append() functionality might be helpful\n",
    "        ### \n",
    "        \n",
    "        return self.grid\n",
    "    \n",
    "    def output_probability(self, grid_point):\n",
    "        \n",
    "        ### TODO:\n",
    "        # Given a point on the grid, such as [0,4] return the\n",
    "        # current probability at that point.\n",
    "        # You will need to use the self.map variable and combine it\n",
    "        # with the grid_point and then return the probability\n",
    "        return \n",
    "    \n",
    "    def update_probability(self, update_list):\n",
    "        \n",
    "        #### TODO:\n",
    "        # Given a list of grid_points and new probabilities, \n",
    "        # update the probabilities of the grid points.\n",
    "        # Here is an example input to this function\n",
    "        # [[3,4,.01], [4,5,.02], [0, 1, .02]]\n",
    "        # This means first update grid point (3,4) to have probability 0.01\n",
    "        # Then update grid point (4,5) to have probability 0.02\n",
    "        # Finally update grid point (0, 1) to have probability 0.02.\n",
    "        # Your function will be updating the elements in the self.map variable\n",
    "        \n",
    "        return self.grid\n",
    "            \n",
    "    def visualize_probability(self):\n",
    "        # this function is given so that you can visualize the results.\n",
    "        # There is no need to change anything.\n",
    "\n",
    "        # this line of code ensures TEST RUN button does not produce an error\n",
    "        # if self.grid is empty.\n",
    "        if not self.grid:\n",
    "            self.grid = [[0],[0]]\n",
    "        else:\n",
    "            plt.imshow(self.grid, cmap='Greys', clim=(0,.1))\n",
    "            plt.title('Heat Map of Grid Probabilities')\n",
    "            plt.xlabel('grid x axis')\n",
    "            plt.ylabel('grid y axis')\n",
    "            plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run the cell below to try out your SelfDrivingCar class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "car = SelfDrivingCar(5,4)\n",
    "\n",
    "car.initialize_grid()\n",
    "\n",
    "# should output 0.05\n",
    "print(car.output_probability([2,3]))\n",
    "\n",
    "# should output 0.05\n",
    "print(car.output_probability([1,2]))\n",
    "\n",
    "car.update_probability([[2,3,.2], [1,2,.1]])\n",
    "\n",
    "# should output 0.2\n",
    "print(car.output_probability([2,3]))\n",
    "\n",
    "# should output 0.1\n",
    "print(car.output_probability([1,2]))\n",
    "\n",
    "# should output a heat map\n",
    "car.visualize_probability()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
